package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class DanmuActivity extends AppCompatActivity {
    private VideoView videoView;
    private DanmakuView danmaku;
    private DanmakuContext danmakuContext;
    private RelativeLayout showInput;
    private Button danmuSend;
    private EditText danmuText;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_danmu);


        if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        videoView = (VideoView) findViewById(R.id.videoView);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        videoView.setLayoutParams(layoutParams);
        danmaku = (DanmakuView) findViewById(R.id.danmaku);
        showInput = (RelativeLayout) findViewById(R.id.showInput);
        danmuSend = (Button) findViewById(R.id.danmu_send);
        danmuText = (EditText) findViewById(R.id.danmuText);

        String uri = "android.resource://"+getPackageName()+"/"+R.raw.blackman;
        if(uri!=null){
            videoView.setVideoURI(Uri.parse(uri));
            videoView.start();
        }else {
            videoView.getBackground().setAlpha(0);
            Toast.makeText(DanmuActivity.this,"视频资源异常，无法播放！",Toast.LENGTH_LONG).show();
        }
        initDanmaku();
        danmaku.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(showInput.getVisibility()==View.GONE){
                    showInput.setVisibility(View.VISIBLE);
                }else{
                    showInput.setVisibility(View.GONE);
                }
            }
        });

        danmuSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String context = danmuText.getText().toString();
                if(!TextUtils.isEmpty(context)){
                    addDanmaku(context,true);
                    danmuText.setText("");
                    Toast.makeText(DanmuActivity.this,"发送成功！",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(DanmuActivity.this,"输入弹幕不能为空！",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    BaseDanmakuParser parser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    private void initDanmaku(){
        danmaku.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                danmaku.start();
                randomDanmu();
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuContext = DanmakuContext.create();
        danmaku.prepare(parser,danmakuContext);
    }
    public static String randomHexStr(int len) {
        try {
            StringBuffer result = new StringBuffer();
            for(int i=0;i<len;i++) {
                //随机生成0-15的数值并转换成16进制
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            return result.toString().toUpperCase();
        } catch (Exception e) {
            System.out.println("获取16进制字符串异常，返回默认...");
            return "00CCCC";
        }
    }
    //添加弹幕
    private void addDanmaku(String context,boolean border){
        BaseDanmaku baseDanmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        baseDanmaku.text = context;
        baseDanmaku.textSize = new Random().nextInt(30)+30;
        baseDanmaku.padding = 10;
        if(border) {
            baseDanmaku.textColor = Color.GREEN;
        }else{
            baseDanmaku.textColor = Integer.valueOf(randomHexStr(6), 16);;
        }
        baseDanmaku.setTime(danmaku.getCurrentTime());
        if(border){
            baseDanmaku.borderColor = Color.RED;
        }
        danmaku.addDanmaku(baseDanmaku);
    }

    private void randomDanmu(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                int num = 1;
                while(num<30){
                    int show = new Random().nextInt(233);
                    String context[] = {"下次一定","2333","666","已经关注了","失踪人口回归","第一","UP主牛逼"
                            ,"专业","专业","专业","专 业 团 队","专业","专业","hahahaha","哈哈哈哈哈"};
                    addDanmaku(context[new Random().nextInt(15)],false);
                    try{
                        Thread.sleep(show);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
